const db = require('../db')
const R = require('../response')

exports.addArticle = (req, res) => {
    // 查看文章是否已经存在
    const articleStr = 'select * from tb_article where title = ? and column_id = ? and is_deleted = 0'
    db.query(articleStr, [req.body.title, req.body.columnId], (err, results) => {
        if (err) {
            return res.send(new R(1, err.message))
        }
        if (results.length > 0) {
            return res.send(new R(1, '该文章已经存在!'))
        }
        // 添加文章
        const insertSql = 'insert into tb_article set ?'
        db.query(insertSql, {
            title: req.body.title,
            excerpt: req.body.excerpt,
            content: req.body.content,
            cover: req.body.cover,
            user_id: req.body.userId,
            column_id: req.body.columnId,
            createdAt: new Date()
        }, (err, results) => {
            if (err) {
                return res.send(new R(1, err.message))
            }
            if (results.affectedRows !== 1) {
                return res.send(new R(1, '添加文章失败!'))
            }
            return res.send(new R(0, '添加文章成功!'))
        })
    })
}

exports.getArticleInfo = (req, res) => {
    const sqlStr = 'select * from tb_article where id = ? and is_deleted = 0'
    db.query(sqlStr, req.params.id, (err, results) => {
        if (err) {
            return res.send(new R(1, err.message))
        }
        if (results.length === 0) {
            return res.send(new R(1, '没有此文章!'))
        }
        // 获取该文章的作者信息
        const userSql = 'select * from tb_user where id = ?'
        db.query(userSql, results[0]?.user_id, (err, result) => {
            if (err) {
                return res.send(new R(1, err.message))
            }
            results[0].user = result[0]
            return res.send(new R(0, '获取文章成功!', results[0]))
        })
    })
}

exports.deleteArticle = (req, res) => {
    const sqlStr = 'update tb_article set is_deleted = 1 where id = ?'
    db.query(sqlStr, req.params.id, (err, results) => {
        if (err) {
            return res.send(new R(1, err.message))
        }
        if (results.affectedRows !== 1) {
            return res.send(new R(1, '删除失败!'))
        }
        return res.send(new R(0, '删除成功!'))
    })
}

exports.getArticleList = (req, res) => {
    const columnId = req.params.columnId
    const query = req.query
    const currentPage = query.currentPage ? query.currentPage : 1
    const pageSize = query.pageSize ? query.pageSize : 5
    const sqlStr = 'select * from tb_article where is_deleted = 0 and column_id = ? limit ?, ?'
    db.query(sqlStr, [columnId, (currentPage - 1) * pageSize, pageSize], (err, results) => {
        if (err) {
            return res.send(new R(1, err.message))
        }
        const countSql = 'select count(*) as count from tb_article where is_deleted = 0 and column_id = ?'
        db.query(countSql, columnId, (err, total) => {
            if (err) {
                return res.send(new R(1, err.message))
            }
            return res.send(new R(0, '获取专栏下文章列表成功!', {
                articles: results,
                total: total[0].count
            }))
        })
    })
}

exports.updateArticleInfo = (req, res) => {
    const updateSql = 'update tb_article set ? where id = ?'
    db.query(updateSql, [req.body, req.body.id], (err, results) => {
        if (err) {
            return res.send(new R(1, err.message))
        }
        if (results.affectedRows !== 1) {
            return res.send(new R(1, '更新文章失败!'))
        }
        return res.send(new R(0, '更新文章成功!'))
    })
}